module.exports = function($scope, $modal, $modalInstance, $resource, activity, info, adress_list, toaster){
    var all_company = {};

    //加载已经分配的部门列表
    $scope.loadPart = function(){
        $resource('/api/as/ggc/volaccount/getForPartList', {}, {}).save({'pid': activity.id}, function (res) {
            if (res.errcode === 0){
                $scope.list = res.data;
                angular.forEach(res.data, function(item){
                    var index = all_company[item.company_code];
                    if(index){
                        $scope.company_list[index - 1].show = false;
                    }
                });
            } else {
                toaster.error({title: '', body: '查询分配信息失败, 请刷新'});
            }
        });
    }

    init();
    function init(){
        $scope.formData = {'is_address': '0'};
        $scope.activity = activity;
        $scope.adress_list = adress_list;
        $resource('/api/as/sc/office/getForGovList', {}, {}).save({'company_id': info.company_id}, function (res) {
            if (res.errcode === 0){
                $scope.company_list = res.data;
                $scope.company_list.push({
                    id: info.id,
                    code: info.company_code,
                    name: info.name
                })
                angular.forEach($scope.company_list, function(item, index){
                    item.show = true;
                    all_company[item.code] = index + 1;
                });
                $scope.loadPart();
            } else {
                toaster.error({title: '', body: '查询机构列表, 请刷新页面'});
            }
        });
    }

    $scope.add = function(){
        if(!$scope.formData.company){
            toaster.warning({title: '', body: '请选择部门'});
            return;
        }
        if(!$scope.formData.num || $scope.formData.num <= 0){
            toaster.warning({title: '', body: '请填写分配名额'});
            return;
        }
        if(!$scope.formData.address_id){
            toaster.warning({title: '', body: '请选择默认乘车地址'});
            return;
        }
        if(activity.tour_stock - activity.use_stock < $scope.formData.num){
            toaster.warning({title: '', body: '剩余名额不足'});
            return;
        }
        var para = {
            'pid': activity.id,
            'company_name': $scope.formData.company.name,
            'company_code': $scope.formData.company.code,
            'all_stock': $scope.formData.num,
            'tour_date': activity.tour_date,
            'activity_id': activity.activity_id,
            'is_address': $scope.formData.is_address,
            'address_id': $scope.formData.address_id
        }
        $resource('/api/ac/ggc/voluntourismAccountService/addAccountChild', {}, {}).save(para, function (res){
            if (res.errcode === 0){
                toaster.success({title: '', body: '添加成功'});
                $scope.loadPart();
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    $scope.update = function(info){
        info.update = true;
    }

    $scope.save = function(info) {
        if(info.tour_stock <= 0){
            toaster.warning({title: '', body: '修改名额错误'});
            return;
        }
        $resource('/api/ac/ggc/voluntourismAccountService/updateAccountChildNum', {}, {}).save(info, function(res){
            if (res.errcode === 0){
                toaster.success({title: '', body: '修改成功'});
                info.update = false;
                $scope.loadPart();
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    $scope.delete = function(info, index){
        if(confirm('确认删除已经分配的名额吗？')){
            $resource('/api/as/ggc/volaccount/deleteAccountChild', {}, {}).save(info, function (res) {
                if (res.errcode === 0){
                    toaster.success({title: '', body: '删除成功'});
                    $scope.list.splice(index, 1);
                    $scope.company_list[all_company[info.login_code] - 1].show = true;
                    // $scope.activity.use_stock -= info.all_stock
                } else {
                    toaster.error({title: '', body: '删除失败, 请重试'});
                }
            });
        }
    }

    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    }


};